// ContainerSel.cpp : implementation file
//

#include "stdafx.h"
#include "VolDia.h"
#include "ContainerSel.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

GENIDX	m_cont;
/////////////////////////////////////////////////////////////////////////////
// CContainerSel dialog


CContainerSel::CContainerSel(CWnd* pParent /*=NULL*/)
	: CDialog(CContainerSel::IDD, pParent)
{
	//{{AFX_DATA_INIT(CContainerSel)
	m_CurrCont = _T("");
	m_HECA = -1;
	m_FALoad = -1;
	m_NoCont = -1;
	m_FAUnload = -1;
	//}}AFX_DATA_INIT
}


void CContainerSel::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CContainerSel)
	DDX_Text(pDX, IDC_CURR_SEL, m_CurrCont);
	DDX_Radio(pDX, IDC_HECA, m_HECA);
	DDX_Radio(pDX, IDC_FA_LOAD, m_FALoad);
	DDX_Radio(pDX, IDC_NO_CONT, m_NoCont);
	DDX_Radio(pDX, IDC_CONT_UNL, m_FAUnload);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CContainerSel, CDialog)
	//{{AFX_MSG_MAP(CContainerSel)
	ON_EN_SETFOCUS(IDC_CURR_SEL, OnSetfocusCurrSel)
	ON_BN_CLICKED(IDC_HECA, OnHeca)
	ON_BN_CLICKED(IDCONFIRM, OnConfirm)
	ON_BN_CLICKED(IDC_FA_LOAD, OnFaLoad)
	ON_BN_CLICKED(IDC_CONT_UNL, OnContUnl)
	ON_BN_CLICKED(IDC_NO_CONT, OnNoCont)
	ON_WM_SHOWWINDOW()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CContainerSel message handlers

void CContainerSel::OnSetfocusCurrSel() 
{
	SetFocus();	
}

BOOL CContainerSel::InitDialog(void){
	RETSTAT		retSt;
	GENIDX		contIdx;
	CONTNAME	contName;

	retSt = GetDynCurrentCont(&contIdx);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unable to retrieve current container Idx."), _T("Error"), MB_OK | MB_ICONERROR);
		return FALSE;
	}

	if(contIdx == Z1_NO_CONT){
		m_FALoad = -1;
		m_FAUnload = -1;
		m_NoCont = 0;
		m_HECA = -1;
	}
	else if(contIdx == Z1_CONT_TK13){
		m_FALoad = -1;
		m_FAUnload = 0;
		m_NoCont = -1;
		m_HECA = -1;
	}
	else if(contIdx == Z1_CONT_LOAD){
		m_FALoad = 0;
		m_FAUnload = -1;
		m_NoCont = -1;
		m_HECA = -1;
	}
	else if(contIdx == Z1_CONT_HECA){
		m_FALoad = -1;
		m_FAUnload = -1;
		m_NoCont = -1;
		m_HECA = 0;
	}
	else{
		m_CurrCont.Format(_T("Unknown container"));
		m_FALoad = -1;
		m_FAUnload = -1;
		m_NoCont = 0;
		m_HECA = -1;
		return TRUE;
	}

	m_cont = contIdx;

	retSt = GetContainName(contIdx, contName);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unable to retrieve current container name."), _T("Error"), MB_OK | MB_ICONERROR);
		return FALSE;
	}

	m_CurrCont.Format(_T("%s"), contName);
	UpdateData(false);

	return TRUE;
}

BOOL CContainerSel::OnInitDialog(){
	CDialog::OnInitDialog();
	
	return(this->InitDialog()); 
}

void CContainerSel::OnConfirm(){
	RETSTAT		retSt;
	if((m_FALoad == -1) && (m_FAUnload == -1)  && (m_HECA == -1)  && (m_NoCont == -1)){
		retSt = SetDynCurrentCont(Z1_CONT_DEFAULT);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Unable to set container."), _T("Error"), MB_OK | MB_ICONERROR);
		}
		else{
			m_cont = Z1_CONT_DEFAULT;
			m_CurrCont.Format(_T("No conatiner"));
			UpdateData(false);
		}
	}

	else if((m_FALoad == 0) && (m_FAUnload == -1)  && (m_HECA == -1)  && (m_NoCont == -1)){
		retSt = SetDynCurrentCont(Z1_CONT_LOAD);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Unable to set container."), _T("Error"), MB_OK | MB_ICONERROR);
		}
		else{
			m_cont = Z1_CONT_LOAD;
			m_CurrCont.Format(_T("FA/Cluster load container"));
			UpdateData(false);
		}
	}
	else if((m_FALoad == -1) && (m_FAUnload == 0)  && (m_HECA == -1)  && (m_NoCont == -1)){
		retSt = SetDynCurrentCont(Z1_CONT_TK13);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Unable to set container."), _T("Error"), MB_OK | MB_ICONERROR);
		}
		else{
			m_cont = Z1_CONT_TK13;
			m_CurrCont.Format(_T("FA/Cluster unload container (TK-13)"));
			UpdateData(false);
		}
	}
	else if((m_FALoad == -1) && (m_FAUnload == -1)  && (m_HECA == 0)  && (m_NoCont == -1)){
		retSt = SetDynCurrentCont(Z1_CONT_HECA);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Unable to set container."), _T("Error"), MB_OK | MB_ICONERROR);
		}
		else{
			m_cont = Z1_CONT_HECA;
			m_CurrCont.Format(_T("Hemetic cask load container"));
			UpdateData(false);
		}
	}
	else if((m_FALoad == -1) && (m_FAUnload == -1)  && (m_HECA == -1)  && (m_NoCont == 0)){
		m_cont = Z1_NO_CONT;
		retSt = SetDynCurrentCont(Z1_NO_CONT);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Unable to set container."), _T("Error"), MB_OK | MB_ICONERROR);
		}
		else{
			m_cont = Z1_NO_CONT;
			m_CurrCont.Format(_T("No conatiner present"));
			UpdateData(false);
		}
	}
}

void CContainerSel::OnFaLoad() 
{
	m_FALoad = 0;
	m_FAUnload = -1;
	m_NoCont = -1;
	m_HECA = -1;
	UpdateData(false);	
}

void CContainerSel::OnContUnl() 
{
	m_FALoad = -1;
	m_FAUnload = 0;
	m_NoCont = -1;
	m_HECA = -1;
	UpdateData(false);	
}

void CContainerSel::OnHeca() 
{
	m_FALoad = -1;
	m_FAUnload = -1;
	m_NoCont = -1;
	m_HECA = 0;
	UpdateData(false);
}

void CContainerSel::OnNoCont() 
{
	m_FALoad = -1;
	m_FAUnload = -1;
	m_NoCont = 0;
	m_HECA = -1;
	UpdateData(false);	
}

void CContainerSel::OnShowWindow(BOOL bShow, UINT nStatus){
	CDialog::OnShowWindow(bShow, nStatus);
	
	this->InitDialog();
	
}
